﻿<%@ WebHandler Language="C#" Class="_member" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using NS.BLL.Systems;
using NS.Utilities;
using WechatUtility;
using BLL;
using Model;

public class _member : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //取得处事类型
        string action = context.Request["ac"];

        switch (action)
        {
            case "info": //获取用户信息
                info(context);
                break;
            case "carList": //获取车辆列表
                carList(context);
                break;
            case "carInfo": //获取车辆详情
                carInfo(context);
                break;
            case "upMobile": //修改手机号
                upMobile(context);
                break;
            case "upInfo": //修改用户信息
                upInfo(context);
                break;
            case "addCar": //新增车辆
                addCar(context);
                break;
            case "upCar": //修改车辆
                upCar(context);
                break;
            case "delCar": //删除车辆
                delCar(context);
                break;
            case "coupons": //获取优惠券列表
                coupons(context);
                break;
            case "cpcount": //获取优惠券数量
                cpcount(context);
                break;
            case "account": //获取账户余额
                account(context);
                break;
            case "accountlist": //获取账户明细
                accountlist(context);
                break;
        }
    }

    private void info(HttpContext context)
    {
        string result = string.Empty;

        Member Model = MemberService.GetMemberByOpenID(CookieHelper.GetValue("openid"));
        if (Model != null)
        {
            MemberCar mc = MemberCarService.GetMemberCarByOpenID(Model.OpenID);
            if (mc != null)
            {
                if (mc.CarTypeID != null)
                {
                    Model.CarTypeID = mc.CarTypeID.Value;
                    Model.CarType = CarTypeService.GetFullNameByTypeID(mc.CarTypeID.Value);
                    Model.CarLogo = CarTypeService.GetLogoByTypeID(mc.CarTypeID.Value);
                }
                else
                {
                    Model.CarTypeID = 0;
                }

                Model.CarNumber = mc.CarNumber;
            }

            string swork = DictionaryService.GetDicValue("工作时间", "开始时间");
            string ework = DictionaryService.GetDicValue("工作时间", "结束时间");
            Model.ServiceTime = swork + ":00 - " + ework + ":00";
            result = JsonHelper.T2JSON(Model);
        }
        context.Response.Write(result);
    }

    private void carList(HttpContext context)
    {
        string result = string.Empty;
        List<MemberCar> list = MemberCarService.GetAllMemberCar(CookieHelper.GetValue("openid"));
        foreach (MemberCar cModel in list)
        {
            cModel.CarType = CarTypeService.GetTypeNameByTypeID(cModel.CarTypeID.Value);
            cModel.CarLogo = WebConfigHelper.GetAppSet("WebUrl") + "/wechat/" + CarTypeService.GetLogoByTypeID(cModel.CarTypeID.Value);

        }
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }
    private void carInfo(HttpContext context)
    {
        string result = string.Empty;
        long id = long.Parse(context.Request["id"]);
        MemberCar m = MemberCarService.GetMemberCarById(id);
        m.CarType = CarTypeService.GetTypeNameByTypeID(m.CarTypeID.Value);
        result = JsonHelper.T2JSON(m);
        context.Response.Write(result);
    }

    private void upMobile(HttpContext context)
    {
        string result = string.Empty;
        string openid = CookieHelper.GetValue("openid");
        string mobile = context.Request["mobile"];

        Member Model = MemberService.GetMemberByOpenID(openid);
        if (Model != null)
        {
            Model.Mobile = mobile;
            MemberService.UpdateMemberByMemberID(Model);
        }
        result = JsonMessager.Success("提交成功");
        context.Response.Write(result);
    }
    private void upInfo(HttpContext context)
    {
        string result = "保存失败";
        string openid = CookieHelper.GetValue("openid");
        string truename = context.Request["truename"];
        string sex = context.Request["sex"];
        string age = context.Request["age"];
        Member Model = MemberService.GetMemberByOpenID(openid);
        if (Model != null)
        {
            Model.TrueName = truename;
            Model.Sex = sex;
            Model.Age = age;
            if (MemberService.UpdateMemberByMemberID(Model) > 0)
            {
                result = string.Empty;
            }
        }
        if (string.IsNullOrEmpty(result))
        {
            result = JsonMessager.Success("保存成功");
        }
        else
        {
            result = JsonMessager.Fail(result);
        }
        context.Response.Write(result);
    }
    private void addCar(HttpContext context)
    {
        string result = string.Empty;
        int ctid = int.Parse(context.Request["ctid"]);
        string carnmber = context.Request["carnmber"];
        string color = context.Request["color"];
        string openid = CookieHelper.GetValue("openid");

        MemberCar Model = new MemberCar();
        Model.OpenID = openid;
        Model.CarTypeID = ctid;
        Model.CarNumber = carnmber;
        Model.Color = color;

        if (MemberCarService.AddMemberCar(Model) > 0)
        {
            result = JsonMessager.Success("添加成功");
        }
        else
        {
            result = JsonMessager.Fail("添加失败");
        }
        context.Response.Write(result);
    }
    private void upCar(HttpContext context)
    {
        string result = string.Empty;

        long id = long.Parse(context.Request["id"]);
        int ctid = int.Parse(context.Request["ctid"]);
        string carnmber = context.Request["carnmber"];
        string color = context.Request["color"];

        MemberCar Model = MemberCarService.GetMemberCarById(id);
        Model.CarTypeID = ctid;
        Model.CarNumber = carnmber;
        Model.Color = color;

        if (MemberCarService.UpdateMemberCarById(Model) > 0)
        {
            result = JsonMessager.Success("保存成功");
        }
        else
        {
            result = JsonMessager.Fail("保存失败");
        }
        context.Response.Write(result);
    }
    private void delCar(HttpContext context)
    {
        string result = string.Empty;
        long id = long.Parse(context.Request["id"]);
        if (MemberCarService.DeleteMemberCarById(id) > 0)
        {
            result = JsonMessager.Success("删除成功");
        }
        else
        {
            result = JsonMessager.Fail("删除失败");
        }
        context.Response.Write(result);
    }

    private void coupons(HttpContext context)
    {
        string result = string.Empty;
        int resInt;
        int couType = int.TryParse(context.Request.QueryString["t"], out resInt) ? resInt : 0;
        int IsUsable = int.TryParse(context.Request.QueryString["u"], out resInt) ? resInt : 0;
        string order = "desc";
        if (!string.IsNullOrEmpty(context.Request.QueryString["od"]))
        {
            order = "";
        }
        List<Coupons> newList = new List<Coupons>();
        List<Coupons> list = CouponsService.GetAllCoupons(CookieHelper.GetValue("openid"), IsUsable, couType, order);
        foreach (Coupons cModel in list)
        {
            if (cModel.IsUse == 1)
            {
                cModel.Tip = "已使用";
                cModel.Css = "stamp04";
            }
            else if (cModel.YouXQEnd.Value < DateTime.Now.AddDays(-1))
            {
                cModel.Tip = "已过期";
                cModel.Css = "stamp04";
            }

        }
        result = JsonHelper.T2JSON(list);
        context.Response.Write(result);
    }
    private void cpcount(HttpContext context)
    {
        string result = string.Empty;
        int cpcount = CouponsService.GetCount(CookieHelper.GetValue("openid"), 0);
        int wcount = CouponsService.GetCount(CookieHelper.GetValue("openid"), 1);

        result = "{'cpcount':'" + cpcount + "','wcount':'" + wcount + "'}";
        context.Response.Write(result);
    }
    private void account(HttpContext context)
    {
        string result = string.Empty;
        string account = MemberAccountService.GetBlance(CookieHelper.GetValue("openid")).ToString();
        string rebate = "0";
        DataTable dt = MemberPayService.GetRebateBlance(CookieHelper.GetValue("openid"));
        if (dt.Rows.Count == 1)
        {
            rebate = dt.Rows[0]["Blance"].ToString();
        }
        result = "{'account':'" + account + "','rebate':'" + rebate + "'}";
        context.Response.Write(result);
    }
    private void accountlist(HttpContext context)
    {
        string result = string.Empty;
        int ps = 10;
        int pi = 1;
        int tp = 0;
        if (!string.IsNullOrEmpty(context.Request["ps"]))
        {
            ps = int.Parse(context.Request["ps"]);
        }
        if (!string.IsNullOrEmpty(context.Request["pi"]))
        {
            pi = int.Parse(context.Request["pi"]);
        }
        DataTable dt = MemberAccountService.GetListByPage(CookieHelper.GetValue("openid"), ps, pi, out tp);
        result = "{tp:'" + tp + "',list:'" + JsonHelper.DataTable2JSON(dt) + "'}";
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}